<?php
/**
 *  Data Access Object
 *
 * @package dao
 * @author 
 **/
class DAO {
	
	/**
	 * 数据库连接实例
	 */
	public static $instance = array ();
	
	/**
	 * 保存执行过的SQL语句
	 *
	 * @var array
	 * @author 
	 **/
	public static $sqls = array ();
	
	/**
	 * 执行过的SQL数量
	 *
	 * @var int
	 **/
	public static $sql_count = 0;
	
	/**
	 * SQL执行时间from
	 *
	 * @var array
	 **/
	public static $sqls_cost_f = array ();
	
	/**
	 * SQL执行时间to
	 *
	 * @var array
	 **/
	public static $sqls_cost_t = array ();
	
	/**
	 * 连接句柄
	 * @access private
	 * @var resource
	 */
	private $_connection = null;
	
	/**
	 * 主机地址
	 *
	 * @var string
	 **/
	private $_host = '127.0.0.1';
	
	/**
	 * 端口地址
	 *
	 * @var string
	 **/
	private $_port = 3306;
	
	/**
	 * 数据库名
	 *
	 * @var string
	 **/
	private $_schema = '';
	
	/**
	 * 用户名
	 *
	 * @var string
	 **/
	private $_user;
	
	/**
	 * 密码
	 *
	 * @var string
	 **/
	private $_password;
	
	/**
	 * 编码
	 *
	 * @var string
	 **/
	private $_charset = 'UTF-8';
	
	/**
	 * 构造方法
	 *
	 * @return Mixed (connection or false)
	 * @author 
	 **/
	public function __construct($host = DBHOST, $port = DBPORT, $schema = DBSCHEMA, $user = DBUSER, $password = DBPASSWORD, $charset = 'UTF-8') {
		$this->_host = $host;
		$this->_port = $port;
		$this->_schema = $schema;
		$this->_user = $user;
		$this->_password = $password;
		$this->_charset = $charset;
		
		if (false == $this->_connect ()) {
			return false;
		} else {
			mysql_query ( "set names utf8" );
			return $this->_connection;
		}
	
	}
	
	/**
	 * 连接数据库
	 * @access private
	 * @return object
	 */
	private function _connect() {
		if (($this->_connection = mysql_pconnect ( $this->_host . ':' . $this->_port, $this->_user, $this->_password )) != false) {
			$this->setCurrentSchema ( $this->_schema );
			mysql_set_charset ( $this->_charset );
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 切换schema
	 * @access public
	 * @param string  $schema
	 * @return boolean
	 */
	public function setCurrentSchema($schema) {
		$this->_schema = $schema;
		if (mysql_select_db ( $this->_schema, $this->_connection )) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * debug 开始
	 * 对SQL的执行数量和内容进行记录
	 * @param $sql string
	 * @return void
	 * @author 
	 **/
	private function _begin_debug($sql) {
		if (defined ( 'DEBUG' ) && DEBUG) {
			self::$sqls [] = $sql;
			self::$sql_count ++;
			self::$sqls_cost_f [] = Utils::getmicrotime ();
		}
	}
	
	/**
	 * debug 结束
	 *
	 * @return void
	 * @author 
	 **/
	private function _end_debug() {
		if (defined ( 'DEBUG' ) && DEBUG) {
			self::$sqls_cost_t [] = Utils::getmicrotime ();
		}
	}
	
	/*============================ db 操作方法 ===========================================*/
	/**
	 * 取一个值
	 * @access public
	 * @param string $sql example : select field_a from table_a Limit 1
	 * @return mixed
	 */
	public function getValue($sql) {
		$this->_begin_debug ( $sql );
		$res = mysql_query ( $sql, $this->_connection );
		$this->_end_debug ();
		$row = mysql_fetch_array ( $res, MYSQL_NUM );
		mysql_free_result ( $res );
		return $row [0];
	}
	
	/**
	 * 取一行(一维数组)
	 * @access public
	 * @param string $sql example : select field_a from table_a Limit 1
	 * @return mixed (array or false)
	 * @author 
	 **/
	public function getRow($sql) {
		$this->_begin_debug ( $sql );
		$res = mysql_query ( $sql, $this->_connection );
		$this->_end_debug ();
		$row = mysql_fetch_assoc ( $res );
		mysql_free_result ( $res );
		return $row;
	}
	
	/**
	 * 取一列(一维数组)
	 * @access public
	 * @param string $sql sql语句
	 * @return array
	 */
	public function getCol($sql) {
		$this->_begin_debug ( $sql );
		$res = mysql_query ( $sql, $this->_connection );
		$this->_end_debug ();
		$data = array ();
		while ( ($row = mysql_fetch_array ( $res, MYSQL_NUM )) != false ) {
			$data [] = $row [0];
		}
		mysql_free_result ( $res );
		return $data;
	}
	
	/**
	 * 取多行(二维数组)
	 * @access public
	 * @param string $sql
	 * @return array
	 */
	public function getPlan($sql) {
		$this->_begin_debug ( $sql );
		$res = mysql_query ( $sql, $this->_connection );
		$this->_end_debug ();
		if ($res == false) {
			return false;
		}
		$data = array ();
		while ( ($row = mysql_fetch_assoc ( $res )) != false ) {
			$data [] = $row;
		}
		mysql_free_result ( $res );
		return $data;
	}
	
	public function getPlan2($sql, $pri = '', $s_pri = '') {
		$this->_begin_debug ( $sql );
		$res = mysql_query ( $sql );
		$this->_end_debug ();
		if ($res == false) {
			return false;
		}
		$data = array ();
		while ( ($row = mysql_fetch_assoc ( $res )) != false ) {
			if ($s_pri) {
				$data [$row [$pri]] [$row [$s_pri]] = $row;
			} else {
				$data [$row [$pri]] = $row;
			}
		}
		mysql_free_result ( $res );
		return $data;
	}
	
	/**
	 * 更新
	 * @param string $sql 更新命令
	 * @return boolean
	 */
	public function Query($sql) {
		$this->_begin_debug ( $sql );
		$res = mysql_query ( $sql, $this->_connection );
		$this->_end_debug ();
		return $res;
	}
	
	/**
	 * 更新
	 * @param string $sql 更新命令
	 * @return boolean
	 */
	public function update($sql) {
		$this->_begin_debug ( $sql );
		$res = mysql_query ( $sql, $this->_connection );
		$this->_end_debug ();
		return $res;
	}
	
	/**
	 * 插入
	 * @param string $sql 插入命令
	 * @return boolean
	 */
	public function insert($sql) {
		$this->_begin_debug ( $sql );
		$res = mysql_query ( $sql, $this->_connection );
		$this->_end_debug ();
		return $res;
	}
	
	/**
	 * 取得上一步 INSERT 操作产生的 ID
	 * @access public
	 * @return integer
	 */
	public function lastID() {
		return mysql_insert_id ( $this->_connection );
	}
	
	/**
	 * 断开连接,在使用mysql_connect函数时使用,
	 * 如果用mysql_pconnect连接则无需使用
	 * @access public
	 * @return boolean
	 */
	public function disconnect() {
		return mysql_close ( $this->_connection );
	}


	public function insertByarr($tbname,$arr){
		$keystr = '';
		$valstr = '';
		$sql = "INSERT INTO ".$tbname;
		foreach ($arr as $key => $value) {
			$keystr .= $key.',';
			$valstr .= "'".mysql_real_escape_string($value)."',";
		}
		$keystr = substr($keystr, 0,strlen($keystr)-1);
		$valstr = substr($valstr, 0,strlen($valstr)-1);
		$sql .= "($keystr) VALUES ($valstr)";
		return $this->insert($sql);
	}


	public function updateByarr($tbname,$keyname,$keyvalue,$arr){
		$keystr = '';
		$valstr = '';
		$sql = "UPDATE ".$tbname.' SET';
		foreach ($arr as $key => $value) {
			$sql .= " $key="."'".mysql_real_escape_string($value)."',";
		}
		$sql = substr($sql, 0,strlen($sql)-1);

		$sql .= " WHERE $keyname='".mysql_real_escape_string($keyvalue)."'";
		//debug($sql);
		return $this->update($sql);
	}


}